<template>
    <div class="login">
        <div class="login-box">
            <div class="logo">
                <img src="static/images/logo.png"/>
            </div>
            <div class="body">
                <p class="tips">欢迎使用NideShop后台管理</p>
                <el-form ref="form" :model="form" :rules="rules" label-position="top">
                    <el-form-item label="" prop="username">
                        <el-input v-model="form.username" placeholder="用户名"></el-input>
                    </el-form-item>
                    <el-form-item label="" prop="password">
                        <el-input type="password" v-model="form.password" placeholder="密码"></el-input>
                    </el-form-item>
                    <el-form-item v-if="form.isRegister">
                        <el-input type="password" v-model="form.register_code" placeholder="注册码"></el-input>
                    </el-form-item>
                    <el-form-item v-if="form.isRegister">
                        <el-button type="primary" @click="register" :loading="loading" style="width: 100%;">
                            {{ loading ? '注册中...' : '注册' }}
                        </el-button>
                    </el-form-item>
                    <el-form-item v-else="!form.isRegister">
                        <el-button type="primary" @click="startLogin" :loading="loading" style="width: 100%;">
                            {{ loading ? '登录中...' : '登录' }}
                        </el-button>
                    </el-form-item>
                    <el-form-item>
                        <el-button @click="change" style="width: 100%;">
                            {{ form.isRegister ? '去登录' : '去注册' }}
                        </el-button>
                    </el-form-item>
                </el-form>

                <p class="author">Created by <a href="https://github.com/yanhang" target="_blank">yanhang0109</a></p>
            </div>
        </div>
    </div>
</template>
<script>
export default {
  data () {
    return {
      form: {
        username: '',
        password: '',
        register_code: '',
        isRegister: false
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, message: '密码不得低于6个字符', trigger: 'blur'}
        ]
      },
      loading: false
    }
  },
  components: {},
  methods: {
    change () {
      this.form.isRegister = !this.form.isRegister
    },
    startLogin () {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return false
        }

        this.loading = true

        this.$db.admin.find({name: this.form.username, password: this.form.password}, (err, docs) => {
          this.loading = false
          if (err) console.error('kumeng: ' + err.stack)
          if (docs && docs.length) {
            localStorage.setItem('token', docs._id)
            this.$router.push({name: 'dashboard'})
          } else {
            this.$message({
              type: 'error',
              message: '登录失败'
            })
          }
        })
      })
    },
    register () {
      this.$refs['form'].validate((valid) => {
        if (!valid || this.form.register_code !== 'yanhangadmin') {
          return false
        }
        this.loading = true
        this.$db.admin.find({name: this.username}, (err, docs) => {
          if (err) console.error('kumeng: ' + err.stack)
          if (docs && docs.length) {
            this.$message({
              type: 'failed',
              message: '用户名已存在，请更换用户名'
            })
            this.loading = false
            return false
          }
          this.$db.admin.insert({name: this.form.username, password: this.form.password}, (err, doc) => {
            if (err) console.error('kumeng: ' + err.stack)
            if (doc) {
              this.$message({
                type: 'success',
                message: '注册成功，请登录'
              })
            }
            this.loading = false
          })
        })
      })
    }
  },
  mounted () {
  },
  name: 'LoginPage'
}
</script>
<style>
    .login {
        align-items: center;
        background: #f5f7f9;
        display: flex;
        font-family: Lato, Helvetica, sans-serif;
        justify-content: center;
        text-align: center;
        height: 100%;
        width: 100%;
        color: #656565;
    }

    .login-box {
        width: 320px;
        height: 350px;
        background: #fff;
    }

    .login-box .logo {
        height: 54px;
        background: #324157;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .login-box .logo img {
        max-height: 30px;
    }

    .login-box .body {
        padding: 15px;
    }

    .login-box .body .tips {
        font-size: 14px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        margin-bottom: 10px;
    }

    .login-box .body .author {
        display: block;
        font-size: 14px;
        height: 40px;
        line-height: 40px;
        text-align: center;
        color: #656565;
        margin-bottom: 10px;
        text-decoration: none;
    }

    .login-box .body .author a {
        text-decoration: none;
    }
</style>
